package com.example.quartzdemo.SpringTaskTestConfig.service.impl;

import com.example.quartzdemo.SpringTaskTestConfig.config.CronTaskRegistrar;
import com.example.quartzdemo.SpringTaskTestConfig.config.SchedulingRunnable;
import com.example.quartzdemo.SpringTaskTestConfig.config.SysJobStatus;
import com.example.quartzdemo.SpringTaskTestConfig.pojo.SysJobPO;
import com.example.quartzdemo.SpringTaskTestConfig.service.SysjonRunnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @TODO
 * @ClassName SysJobRunnerServiceImpl
 * @Author 81373
 * @Date 2022/11/10 0:20
 */
@Service
@Slf4j
public class SysJobRunnerServiceImpl implements SysjonRunnerService {

    @Autowired
    CronTaskRegistrar cronTaskRegistrar;

    /**
      * @TODO   新增定时任务
      * @Author 江
      * @Date 2022/11/20 14:27
      */
    @Override
    public  String addScheduled(SysJobPO sysJobPO){
      /*  sysJobPO.setCreateTime(new Date());
        sysJobPO.setUpdateTime(new Date());
        int success = sysJobPOMapper.insert(sysJobPO);
        if (success != 1)
            return "新增失败";*/
//        else {
        System.out.println("sysJobPO = " + sysJobPO);
        if (sysJobPO.getJobStatus() == 1) {
            SchedulingRunnable task = new SchedulingRunnable(sysJobPO.getBeanName(), sysJobPO.getMethodName(),
                    sysJobPO.getMethodParams());
            cronTaskRegistrar.addCronTask(task, sysJobPO.getCronExpression());
        }
//        }
        return "OK";

    }

    /**
      * @TODO   修改定时任务 : 先移除原来的任务，再启动新任务
      * @Author 江
      * @Date 2022/11/20 14:28
      */
    public void editSysJob(SysJobPO sysJob) {

//        SysJobPO sysJobPO = new SysJobPO();
//        BeanUtils.copyProperties(sysJob, sysJobPO);
//        sysJobPO.setUpdateTime(new Date());

//        SysJobPO existedSysJob = sysJobPOMapper.selectById(sysJob.getJobId());
        //先移除
        SchedulingRunnable task = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(),
                "6666");
        cronTaskRegistrar.removeCronTask(task);

//        sysJob.setCreateTime(existedSysJob.getCreateTime());
        try {
//            sysJobPOMapper.insert(sysJobPO);
            //然后判断是否开启  1
            if (sysJob.getJobStatus().equals(SysJobStatus.NORMAL.ordinal())) {
                SchedulingRunnable task2 = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(),
                        sysJob.getMethodParams());
                cronTaskRegistrar.addCronTask(task2, sysJob.getCronExpression());
            }
        } catch (Exception e) {
            log.info("-----编辑定时任务失败!", e);
//            throw new CronException("编辑定时任务失败!");
        }
    }

    /**
      * @TODO       删除定时任务
      * @Author 江
      * @Date 2022/11/20 14:36
      */
    @Override
    public String deleteSysJobById(SysJobPO sysJob) {
        /* public void deleteSysJobById(Integer jobId) {
        SysJobPO existedSysJob = sysJobPOMapper.selectById(jobId);
        try {
            sysJobPOMapper.deleteById(jobId);
        } catch (Exception e) {
            log.info("-----删除定时任务失败!", e);
//            throw new CronException("删除定时任务失败!");
        }
        SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getBeanName(), existedSysJob.getMethodName(),
                existedSysJob.getMethodParams());*/
        SchedulingRunnable task3 = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(),
                sysJob.getMethodParams());
        cronTaskRegistrar.removeCronTask(task3);
        return "OK";
    }
}
